package Screens;

import Utils.SoundManager;

import com.badlogic.gdx.Screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.acumenvn.pent.MyPent;

public class MenuInGameScreen implements Screen {

	Sound mSoundSwitch;
	Sound mSoundSelect;
	
	MyPent mGame;
	Texture mTextureBackground;
	Texture mTextureSelector;
	Sprite mSpriteBackground;
	Sprite mSpriteSelector;
	SpriteBatch mSpriteBatch;
	InputProcessor mInputProcessor = new InputProcessor() {
		
		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean touchDown(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean scrolled(int amount) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyUp(int keycode) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyTyped(char character) {
			// TODO Auto-generated method stub
			return false;
		}
		
		@Override
		public boolean keyDown(int keycode) {
			// TODO Auto-generated method stub
			switch (keycode) {
			case Keys.UP:
				mSelectorMenuIndex--;
				mPointer_Y += mJumpMenu;
				handleMenuPointer();
				break;
			case Keys.DOWN:
				mSelectorMenuIndex++;
				mPointer_Y -= mJumpMenu;
				handleMenuPointer();
				break;
			case Keys.ENTER:
				handleInputSelector();
				break;
			default:
				break;
			}
			return false;
		}
	};
	Boolean keyPressed = false;
	
	int mSelectorMenuIndex = 0;
	int mJumpMenu = 60;
	int mPointer_X = 0;
	int mPointer_Y = 0;
	
	public MenuInGameScreen(MyPent game) {
		// TODO Auto-generated constructor stub
		this.mGame = game;
	}
	
	@Override
	public void render(float delta) {
		// TODO Auto-generated method stub
		Gdx.gl20.glClearColor(0, 0, 0, 1);
		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		mSpriteBatch.begin();
		
		mSpriteBackground.draw(mSpriteBatch);
		mSpriteSelector.draw(mSpriteBatch);
		mSpriteBatch.end();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		int screenHeight = Gdx.graphics.getHeight();
		int screenWidth = Gdx.graphics.getWidth();
		mSpriteBatch = new SpriteBatch();
		// MenuInGame
		mTextureBackground = new Texture("Images/MenuInGame.png");
		mTextureBackground.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mSpriteBackground = new Sprite(mTextureBackground);
		mSpriteBackground.setPosition(0, 0);
		mSpriteBackground.setOrigin(mSpriteBackground.getWidth()/2, mSpriteBackground.getHeight()/2);
		mSpriteBackground.setPosition(screenWidth/2 - mSpriteBackground.getWidth()/2, screenHeight/2 - mSpriteBackground.getHeight()/2);
		
		// Selector
		mTextureSelector = new Texture("Images/BorderInGame.png");
		mTextureSelector.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		mSpriteSelector = new Sprite(mTextureSelector);
		mPointer_X = (int) (mSpriteBackground.getX() + 10);
		mPointer_Y = (int) (mSpriteBackground.getY() + 70);
		mSpriteSelector.setPosition(mPointer_X, mPointer_Y);
		
		mSelectorMenuIndex = 2;
		Gdx.input.setInputProcessor(mInputProcessor);
		Gdx.graphics.setContinuousRendering(false);
		Gdx.graphics.requestRendering();
		
		mSoundSelect = SoundManager.getInstance().getSoundSelectMenu();
		mSoundSwitch = SoundManager.getInstance().getSoundSwitchMenu();
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		mTextureBackground.dispose();
		mTextureSelector.dispose();
		mSpriteBatch.dispose();
	}
	
	private void handleMenuPointer() {
		SoundManager.getInstance().playSound(mSoundSwitch);
		if(mSelectorMenuIndex > 2) {
			mSelectorMenuIndex = 0;
			mPointer_Y = (int)(mSpriteBackground.getY() + 70) + 2*mJumpMenu;
		}
		
		if(mSelectorMenuIndex < 0) {
			mSelectorMenuIndex = 2;
			mPointer_Y = (int) (mSpriteBackground.getY() + 70);
		}

		mSpriteSelector.setPosition(mPointer_X, mPointer_Y);
	}
	
	private void handleInputSelector() {
		SoundManager.getInstance().playSound(mSoundSelect);
		switch(mSelectorMenuIndex) {
		case 0:
			// Resume
			this.mGame.setScreen(new GameScreen(this.mGame));
			break;
		case 1:
			// Save game
			break;
		case 2:
			// Quit game
			this.mGame.setScreen(new MainMenuScreen(this.mGame));
			break;
		default:
			break;
		}
	}

}
